package com.shangcheng.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.shangcheng.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.shangcheng.dubbo.api.SmsFlashPromotionSessionApi;
import com.shangcheng.model.domain.SmsFlashPromotionProductRelation;
import com.shangcheng.model.domain.SmsFlashPromotionSession;
import com.shangcheng.model.vo.SmsFlashPromotionSessionVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsFlashPromotionSessionService {
    @DubboReference
    private SmsFlashPromotionSessionApi smsFlashPromotionSessionApi;

    @DubboReference
    private SmsFlashPromotionProductRelationApi smsFlashPromotionProductRelationApi;

    /**
     * 添加场次
     *
     * @param smsFlashPromotionSession
     */
    public void create(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionApi.save(smsFlashPromotionSession);
    }

    /**
     * 删除数据
     *
     * @param id
     */
    public void delete(Integer id) {
        smsFlashPromotionSessionApi.delete(id);
    }

    /**
     * 修改启用状态
     *
     * @param id
     * @param status
     */
    public void updateStatus(Integer id, Integer status) {
        //1.根据id进行查询
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionApi.findById(id);
        //2.进行数据是否有的判断
        if (ObjectUtil.isEmpty(smsFlashPromotionSession)) return;
        //3.对smsFlashPromotionSession进行数据封装
        smsFlashPromotionSession.setStatus(status);
        //4.修改数据
        smsFlashPromotionSessionApi.update(smsFlashPromotionSession);
    }

    /**
     * 修改场次信息
     *
     * @param smsFlashPromotionSession
     */
    public void update(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionApi.update(smsFlashPromotionSession);
    }

    /**
     * 产看详情
     *
     * @param id
     * @return
     */
    public SmsFlashPromotionSession findById(Integer id) {
        return smsFlashPromotionSessionApi.findById(id);
    }

    /**
     * 获取场次数据
     *
     * @return
     */
    public List<SmsFlashPromotionSession> findList() {
        List<SmsFlashPromotionSession> SmsFlashPromotionSessions = smsFlashPromotionSessionApi.findAll();
        return SmsFlashPromotionSessions;
    }

    /**
     * 获取全部可选场次及其数量
     *
     * @return
     */
    public List<SmsFlashPromotionSessionVo> selectList(Integer flashPromotionId) {
        //1.获取全部场次
        List<SmsFlashPromotionSession> SmsFlashPromotionSessions = this.findList();
        //2.利用各个场次计算出每个smsFlashPromotionSession中所对应的商品数量
        List<SmsFlashPromotionSessionVo> vos = new ArrayList<>();
        for (SmsFlashPromotionSession smsFlashPromotionSession : SmsFlashPromotionSessions) {
            Integer count = smsFlashPromotionProductRelationApi.findAllProductCountByFlashCondition(flashPromotionId,smsFlashPromotionSession.getId());
            if (smsFlashPromotionSession!=null){
                SmsFlashPromotionSessionVo vo = SmsFlashPromotionSessionVo.init(smsFlashPromotionSession, count);
                vos.add(vo);
            }
        }
        return vos;
    }
}
